//
//  RegistTwoViewController.m
//  XDCommonApp
//
//  Created by wanglong8889@126.com on 14-3-6.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "RegistTwoViewController.h"
#import "XDTools.h"
#import "XDHeader.h"
#import "RegistThreeViewController.h"
#define XZB     65
@interface RegistTwoViewController ()

@end

@implementation RegistTwoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initViews];
	// Do any additional setup after loading the view.
}
-(void)initViews{
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width
                                                                     , self.contentView.frame.size.height)];
    _scrollView.scrollEnabled = NO;
    [self.contentView addSubview:_scrollView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelPan)];
    [self changeFrameWhenHiddenNavigationBar];
    [self.contentView addGestureRecognizer:tap];
    
    UIImageView *pointView = [[UIImageView alloc] initWithFrame:CGRectMake(122, 22, 72, 11)];
    pointView.image = [UIImage imageNamed:@"second_point"];
    [self.scrollView addSubview:pointView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(XZB, 90,110, 25)];
    titleLabel.text = @"设置密码";
    titleLabel.font = [UIFont fontWithName:@"Helvetica" size:22.0];
    titleLabel.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:titleLabel];
    
    UILabel *pwdLabel = [[UILabel alloc] initWithFrame:CGRectMake(XZB, titleLabel.frame.origin.y+titleLabel.frame.size.height+38,60, 15)];
    pwdLabel.text = @"输入密码";
    pwdLabel.font = [UIFont systemFontOfSize:13.0];
    pwdLabel.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:pwdLabel];
    
    pwdTf = [[UITextField alloc] initWithFrame:CGRectMake(XZB, pwdLabel.frame.origin.y+pwdLabel.frame.size.height+10,187, 28)];
    pwdTf.borderStyle = UITextBorderStyleLine;
    pwdTf.backgroundColor =  RGBA(168, 168, 168, 1.0);
    pwdTf.textColor = [UIColor whiteColor];
    if (IOS7) {
        pwdTf.tintColor = [UIColor whiteColor];
        
    }
    pwdTf.delegate = self;
    pwdTf.secureTextEntry = YES;
    pwdTf.font = [UIFont systemFontOfSize:13.0];
    pwdTf.placeholder = @"请输入密码";
    pwdTf.returnKeyType = UIReturnKeyDone;
    pwdTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.scrollView addSubview:pwdTf];
    
    UILabel *confirmLabel = [[UILabel alloc] initWithFrame:CGRectMake(XZB, pwdTf.frame.origin.y+pwdTf.frame.size.height+38,90, 15)];
    confirmLabel.text = @"重新输入密码";
    confirmLabel.font = [UIFont systemFontOfSize:13.0];
    confirmLabel.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:confirmLabel];
    
    confirmTf = [[UITextField alloc] initWithFrame:CGRectMake(XZB, confirmLabel.frame.origin.y+confirmLabel.frame.size.height+10,187, 28)];
    confirmTf.borderStyle = UITextBorderStyleLine;
    confirmTf.backgroundColor =  RGBA(168, 168, 168, 1.0);
    confirmTf.placeholder = @"请再次输入密码";
    confirmTf.textColor = [UIColor whiteColor];
    if (IOS7) {
        confirmTf.tintColor = [UIColor whiteColor];
        
    }
    confirmTf.delegate = self;
    confirmTf.font = [UIFont systemFontOfSize:13.0];
    confirmTf.returnKeyType = UIReturnKeyDone;
    confirmTf.secureTextEntry = YES;
    confirmTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.scrollView addSubview:confirmTf];
    
    
    UILabel *continueLabel = [[UILabel alloc] initWithFrame:CGRectMake(XZB+135, confirmTf.frame.origin.y+confirmTf.frame.size.height+31, 30,15)];
    continueLabel.text = @"完成";
    continueLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0];
    continueLabel.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:continueLabel];
    
    UIImageView *arImageView = [[UIImageView alloc] initWithFrame:CGRectMake(continueLabel.frame.origin.x+continueLabel.frame.size.width+6, continueLabel.frame.origin.y-2, 12, 18)];
    arImageView.image = [UIImage imageNamed:@"launh_sure"];
    [self.scrollView addSubview:arImageView];
    
    UIButton *continueBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    continueBtn.frame = CGRectMake(XZB+130, confirmTf.frame.origin.y+confirmTf.frame.size.height+26, 50,28);
    [continueBtn addTarget:self action:@selector(continueNex) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:continueBtn];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(8,0, 45, 45);
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *bImageView = [[UIImageView alloc] initWithFrame:CGRectMake(24, 17, 11, 18)];
    bImageView.image = [UIImage imageNamed:@"left_back"];
    [self.scrollView addSubview:bImageView];
    [self.scrollView addSubview:backBtn];
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)cancelPan{
    [pwdTf resignFirstResponder];
    [confirmTf resignFirstResponder];
    [UIView animateWithDuration:.3 animations:^{
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }];
    
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self cancelPan];
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [UIView animateWithDuration:.3 animations:^{
        self.scrollView.contentOffset = CGPointMake(0, 60);
    }];
    
    return  YES;
}
//完成
-(void)continueNex{
    if (!pwdTf.text.length || !confirmTf.text.length) {
        [XDTools showTips:@"密码不能为空" toView:self.view];
        return;
    }
    if ([pwdTf.text isEqualToString:confirmTf.text]&&pwdTf.text.length!=0) {
        if ([XDTools NetworkReachable]){
            NSString *body = [NSString stringWithFormat:@" <RegisterTwo xmlns=\"http://tempuri.org/\">"
                              "<phone>%@</phone>"
                              "<pwd>%@</pwd>"
                              "<Uid>%@</Uid>"
                              "</RegisterTwo>",[WLSingleton sharedManager].phoneName,confirmTf.text,[WLSingleton sharedManager].Uid];
            
            __weak ASIFormDataRequest *request = [XDTools postRequestWithDict:body API:REGISTERTWO];
            [request setCompletionBlock:^{
                [XDTools hideProgress:self.contentView];
                //[XDTools hideProgress:self.contentView];
                NSString *responseString = [request responseString];
                // NSDictionary *tempDic = [XDTools  JSonFromString:responseString];
                DDLOG(@"responseString:%@", responseString);
                
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                int statusCode = [request responseStatusCode];
                NSString *soapAction = [[request requestHeaders] objectForKey:@"SOAPAction"];
                
                NSArray *arraySOAP =[soapAction componentsSeparatedByString:@"/"];
                int count = [arraySOAP count] - 1;
                NSString *methodName = [arraySOAP objectAtIndex:count];
                
                // Use when fetching text data
                NSString *result = nil;
                if (statusCode == 200) {
                    //表示正常请求
                    result = [SoapXmlParseHelper SoapMessageResultXml:responseString ServiceMethodName:methodName];
                    NSDictionary  *responseDict = [XDTools JSonFromString:result];
                    DDLOG(@"respodic = %@",responseDict);
                    if ([[responseDict objectForKey:@"code"] intValue]==0){
                        [WLSingleton sharedManager].pwd = pwdTf.text;
                        RegistThreeViewController *threeVC = [[RegistThreeViewController alloc] init];
                        [self.navigationController pushViewController:threeVC animated:YES];
                    }else{
                        [XDTools showTips:@"密码设置失败" toView:self.contentView];
                    }
                }
            }];
            
            [request setFailedBlock:^{
                [XDTools hideProgress:self.contentView];
            }];
            
            [request startAsynchronous];
            [XDTools showProgress:self.contentView];
        }else{
            [XDTools showTips:brokenNetwork toView:self.contentView];
        }
    }else{
        [XDTools showTips:@"两次密码不一致" toView:self.contentView];
    }
   
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
